﻿using System;
using System.Data;
using System.Data.Common;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;

namespace Besture.UI.Admin.CommonModel.AutoMatic
{
    public partial class USER_TBTeamSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id <= 0)
            {
                return;
            }
            DataTable table = QueryDac.GetTable("SELECT * FROM TBTeam  WHERE ID = " + id);
            if (table.Rows.Count == 0)
                return;
            DataRow row = table.Rows[0];
			Name.Text = row["Name"].ToString();Email.Text = row["Email"].ToString();TELL.Text = row["TELL"].ToString();phone.Text = row["phone"].ToString();WorkLanguage.Text = row["WorkLanguage"].ToString();Remarks.Text = row["Remarks"].ToString();Photo.ImageUrl = row["Photo"].ToString();address.Text = row["address"].ToString();
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            int identityId = -1;
            //添加
            if (!EditPage)
            {
                BaseDataAccess baseDataAccess = new BaseDataAccess();

                string insertSql =
                        @"INSERT INTO TBTeam
                        (
                        	Name,Email,TELL,phone,WorkLanguage,Remarks,Photo,address
	                       
                        )
                        VALUES
                        (
                        	@Name,@Email,@TELL,@phone,@WorkLanguage,@Remarks,@Photo,@address
	                        
                        )";

                try
                {
                    baseDataAccess.CreateConnection();
                    DbCommand ldcDBCommand = baseDataAccess.GetCommand(insertSql);
                    
                    baseDataAccess.SetParameter(ldcDBCommand, "@Name", DbType.String, Name.Text);baseDataAccess.SetParameter(ldcDBCommand, "@Email", DbType.String, Email.Text);baseDataAccess.SetParameter(ldcDBCommand, "@TELL", DbType.String, TELL.Text);baseDataAccess.SetParameter(ldcDBCommand, "@phone", DbType.String, phone.Text);baseDataAccess.SetParameter(ldcDBCommand, "@WorkLanguage", DbType.String, WorkLanguage.Text);baseDataAccess.SetParameter(ldcDBCommand, "@Remarks", DbType.String, Remarks.Text);baseDataAccess.SetParameter(ldcDBCommand, "@Photo", DbType.String, Photo.ImageUrl);baseDataAccess.SetParameter(ldcDBCommand, "@address", DbType.String, address.Text);
                    
                    
                    identityId = baseDataAccess.ExecuteNonQuery(ldcDBCommand);
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    baseDataAccess.CloseConnection();
                }


            }
            //编辑
            else
            {
                BaseDataAccess baseDataAccess = new BaseDataAccess();
                string updateSql =
                       @"UPDATE TBTeam
                        SET
							Name = @Name,Email = @Email,TELL = @TELL,phone = @phone,WorkLanguage = @WorkLanguage,Remarks = @Remarks,Photo = @Photo,address = @address
	                        
	                    WHERE id = @id    
	                    ";
						
                try
                {
                    baseDataAccess.CreateConnection();
                    DbCommand ldcDBCommand = baseDataAccess.GetCommand(updateSql);
                    baseDataAccess.SetParameter(ldcDBCommand, "@Name", DbType.String, Name.Text);baseDataAccess.SetParameter(ldcDBCommand, "@Email", DbType.String, Email.Text);baseDataAccess.SetParameter(ldcDBCommand, "@TELL", DbType.String, TELL.Text);baseDataAccess.SetParameter(ldcDBCommand, "@phone", DbType.String, phone.Text);baseDataAccess.SetParameter(ldcDBCommand, "@WorkLanguage", DbType.String, WorkLanguage.Text);baseDataAccess.SetParameter(ldcDBCommand, "@Remarks", DbType.String, Remarks.Text);baseDataAccess.SetParameter(ldcDBCommand, "@Photo", DbType.String, Photo.ImageUrl);baseDataAccess.SetParameter(ldcDBCommand, "@address", DbType.String, address.Text);
                    baseDataAccess.SetParameter(ldcDBCommand, "@id", DbType.Int32, ID);
                    identityId = baseDataAccess.ExecuteNonQuery(ldcDBCommand);
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    baseDataAccess.CloseConnection();
                }
            }
            if (identityId > 0)
            {
                FunHelper.Alert("保存成功", "USER_TBTeamList.aspx");
            }
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
        
        }

        #endregion
    }
}